<template>
    <el-scrollbar height="550px">
        <el-form :model="adress" label-position="right" label-width="auto">
            <el-row :gutter="10">
                <el-col :span="12">
                    <el-form-item label="姓名" prop="name">
                        <el-input v-model="adress.name" placeholder="请输入姓名" clearable/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="手机号" prop="author">
                        <el-input v-model="adress.phone" placeholder="请输入手机号" clearable/>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row :gutter="10">
                <el-col :span="12">
                    <el-form-item label="地区" prop="date">
                        <el-input v-model="adress.region" placeholder="请输入地区" clearable/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="详细地址" prop="press">
                        <el-input v-model="adress.address" placeholder="请输入详细地址" clearable/>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="10">
                <el-form-item>
                    <template #label>
                        是否默认收货地址
                    </template>
                    <el-select v-model="adress.is_default" placeholder="默认收货地址" style="width: 150px">
                        <el-option label="否" value="0"/>
                        <el-option label="是" value="1"/>
                    </el-select>
                </el-form-item>
            </el-row>
        </el-form>

        <el-row>
            <el-col>
                <div style="text-align: center;">
                    <el-button type="primary" @click="doSave">保存</el-button>
                    <el-button @click="doCancel">取消</el-button>
                </div>
            </el-col>
        </el-row>
    </el-scrollbar>
</template>

<script setup lang="ts">
import {inject, ref, defineEmits} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits([
    "closeDialog",	// 关闭 dialog
    "flushPage"		// 刷新页面
]);

const adress = ref({
    id:'',
    user_id:'',
    name:'',// 姓名
    phone:'',// 手机号
    address:'',// 详细地址
    region:'',// 省、市、区
    is_default: '',// 是否默认
});

const handleUpdateData = (obj) => {
    adress.value = obj;
}

// 暴露给父组件使用的方法
defineExpose({
    handleUpdateData,
})

// 取消
const doCancel = () => {
    emits("closeDialog");
}

// 更新
const doSave = async () => {
    const {data} = await axios.put('/api/user_adress', adress.value);
    if (data > 0) {
        emits("flushPage");				// 触发事件： 刷新页面
        emits("closeDialog");			// 触发事件：关闭 dialog
        ElMessage.success("操作成功！");
    } else {
        ElMessage.error("操作失败！");
    }
}
</script>

<style scoped>
</style>